public with sharing class WeekPlanEdit {

    public String rtSPId{get;set;}
    public String rtId{get;set;}
    public String rtHZId{get;set;}
    public Weekly_Plan__c wpc {get;set;}
    public List<Event> taskList {get;set;}
    public String strjson {get;set;}
    public Event t {get;set;}
    public RecordType rt {get;set;}
    public Boolean isEdit {get;set;}
    public String tabUrl; 
    public string View{get;set;}
    public String regioncode {get; set;}
    private Id CamEventId;
    private string rtWpId;

    public WeekPlanEdit(ApexPages.StandardController con) {
        // rt=[select id from RecordType where DeveloperName='Visit'];
        //Weekly_Plan__c w=(Weekly_Plan__c)con.getRecord();
        t=new Event();

        RecordType rtHZ=[select id from RecordType where DeveloperName='Meeting'];
        RecordType rt=[select id from RecordType where DeveloperName='Visit'];
        RecordType rtSP=[select id from RecordType where DeveloperName='SalesPlanEdit'];
        RecordType rtWp = [select id from RecordType where  DeveloperName='WorkArrangement'];
        rtWpId = rtWp.Id;

        rtSPId=rtSP.Id;
        rtId=rt.Id;
        rtHZId=rtHZ.Id;
         CamEventId= Utils.getRecordTypeIdByDevNameAndObjName('CamEvent', 'Event');

        /*
,CurrentTravelResult__c,BusinessBlanace__c,Current_Business__c
             Business_Budget__c,CurrentBusinessResult__c,Travel_Result__c,TravelBlanace__c,Travel_Budget__c,Business_Result__c
        */
        regioncode = [select AreaCode__c from User where Id =:System.UserInfo.getUserId() ].AreaCode__c;
        wpc=[select Id,Name,Status__c,OwnerId,Start_Date__c,End_Date__c,Current_Travel__c,
            Travel_Total__c,Business_Total__c
             from Weekly_Plan__c where id=:con.getId()];
        //taskList=new List<Event>();
        if(wpc.Status__c=='已提交'){View='none';}
        taskList=[SELECT WhatId,Is_View__c,Is_dele__c,
                  Campaigns__r.Name,
                  Opportunity__c,Opportunity__r.Name,
                  WhoId,Who.Name,
                  Account__c,Account__r.Name,
                  Subject,
                  Type,
                  Contact__c,
                  Contact__r.Name,
                  Work_Address__c,
                  StartDateTime,EndDateTime,Week_Plan__c,Travel_Fee__c, Busness_Fee__c
                  FROM Event WHERE     
                  Week_Plan__c=:wpc.Id 
                  ORDER BY StartDateTime];

        //select出事件不能删除，只能新增
        for(Event eve:taskList)
        {
            eve.Is_View__c='none';
            eve.Is_dele__c='none';
            String sdtstr = String.valueOf(eve.StartDateTime);
            String edtstr = String.valueOf(eve.EndDateTime);
            eve.StartDateTimeStr__c=sdtstr.substring(0, sdtstr.length()-3);
            eve.EndDateTimeStr__c=edtstr.substring(0, edtstr.length()-3);
            //eve.Datastr__c=eve.StartDateTime.format('yyyy-mm-dd');
        }
        if(taskList.size()!=0)
        {
            taskList[taskList.size()-1].Is_View__c='';
        }
        if (System.today() > wpc.End_Date__c) {
            isEdit = false;
        } else {
            isEdit = true;
        }
    }

    public PageReference save(){
        update wpc;
        List<Object> inJsonMap = (List<Object>)JSON.deserializeUntyped(strjson);
        List<Event> taskIncomList=new List<Event>();
        for(Object obj : inJsonMap){
            Map<String,Object> objmap=(Map<String,Object>)obj;
            Event taskIncom=new Event();
            System.debug('*** objmap: ' + objmap + '');
            
                taskIncom.RecordTypeId=rtId;
                if(objmap.get('opp')+''!=''){
                    //如果业务机会不能为空
                    taskIncom.WhatId=Id.valueOf(String.valueOf(objmap.get('opp')));
                    taskIncom.Opportunity__c=String.valueOf(objmap.get('opp'));
                }/*else if(objmap.get('acc')+''!= ''){
                    //客户不为空
                    taskIncom.WhatId=String.valueOf(objmap.get('acc'));
                }else {
                    taskIncom.WhatId=null;
                    taskIncom.Opportunity__c=null;
                }*/
                if(objmap.get('con')+''!=''){
                    //如果联系人不为空
                    taskIncom.Contact__c = String.valueOf(objmap.get('con'));
                   // taskIncom.WhoId=String.valueOf(objmap.get('con'));
                    
                }
                
                if(objmap.get('acc')+'' != ''){
                    taskIncom.Account__c=String.valueOf(objmap.get('acc'));   
                }else {

                    taskIncom.Account__c=null;

                }

            

            if(String.valueOf(objmap.get('type')).equals('参加展会')){
                taskIncom.RecordTypeId=rtHZId; 
            }
            if(String.valueOf(objmap.get('type')).equals('例行拜访/沟通') || String.valueOf(objmap.get('type')).equals('回款拜访/沟通') || String.valueOf(objmap.get('type')).equals('销售拜访/沟通')){
                taskIncom.RecordTypeId=rtId; 
            }
            if(String.valueOf(objmap.get('type')).equals('培训学习') || String.valueOf(objmap.get('type')).equals('整理资料') || String.valueOf(objmap.get('type')).equals('内部会议')){
                taskIncom.RecordTypeId=rtWpId ; 
            }
            if(String.valueOf(objmap.get('type')).equals('市场沟通')
             || String.valueOf(objmap.get('type')).equals('意向') 
             || String.valueOf(objmap.get('type')).equals('合作细节谈判')
             || String.valueOf(objmap.get('type')).equals('提起合作确认')
             || String.valueOf(objmap.get('type')).equals('市场管理')){
                taskIncom.RecordTypeId=CamEventId ; 
            }

            if(objmap.get('enentId')!='')
            {
                taskIncom.Id=String.valueOf(objmap.get('enentId'));
            }
            taskIncom.Subject=String.valueOf(objmap.get('sub'));
            taskIncom.Type=String.valueOf(objmap.get('type'));
            taskIncom.Work_Address__c=String.valueOf(objmap.get('address'));
            taskIncom.StartDateTime =DateTime.valueOf(''+objmap.get('startvisitdate')+':00');
            taskIncom.EndDateTime=DateTime.valueOf(''+objmap.get('endvisitdate')+':00');

            taskIncom.Travel_Fee__c=0;
            taskIncom.Busness_Fee__c=0;

            taskIncom.AccountBudget__c=0;
            taskIncom.AccountBudgetUsed__c=0;
            if(objmap.get('tf')+''!='')
            {
                taskIncom.Travel_Fee__c=Double.valueOf(objmap.get('tf'));
            }
            if(objmap.get('bf')+''!='')
            {
                taskIncom.Busness_Fee__c=Double.valueOf(objmap.get('bf'));
            }



            if(objmap.get('accBudget') +''!=''){
                taskIncom.AccountBudget__c =Double.valueOf(objmap.get('accBudget'));
            }

            if(objmap.get('accBudgetUsed') +''!=''){
                taskIncom.AccountBudgetUsed__c =Double.valueOf(objmap.get('accBudgetUsed'));
            }
            taskIncom.Status__c= '未开始';
            //设置活动来自周计划
            taskIncom.FromWeeklyPlan__c=true;
            taskIncom.Week_Plan__c=wpc.Id;

            taskIncomList.add(taskIncom);

        }
        try{
            System.debug('*** taskIncomList: ' + taskIncomList + '');
            upsert taskIncomList Id;

        }catch(Exception e){

            ApexPages.addMessage(
                new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage()));
            return null;
        }

        return new PageReference('/'+wpc.Id);
    }

    public PageReference cancle(){
        return new PageReference('/'+wpc.Id);
    }
}